package com.lufei4.chestnut.orbyun;

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyChartView extends View {

    // 1.创建一个画笔
    private Paint mPaint = new Paint();
    private Paint mPaintText = new Paint();
    private List<Entry> data = new ArrayList<>();

    // 2.初始化画笔
    private void initPaint() {
        mPaint.setColor(Color.BLACK);       //设置画笔颜色
        mPaint.setStyle(Paint.Style.STROKE);  //设置画笔模式为填充
        mPaint.setStrokeWidth(1f);         //设置画笔宽度为10px
        mPaint.setAntiAlias(true);

        mPaintText.setColor(Color.BLUE);       //设置画笔颜色
        mPaintText.setStyle(Paint.Style.STROKE);  //设置画笔模式为填充
        mPaintText.setStrokeWidth(1f);         //设置画笔宽度为10px
        mPaintText.setAntiAlias(true);

        data.add(new Entry(1, 9));
        data.add(new Entry(2, 5));
        data.add(new Entry(3, 7));
        data.add(new Entry(4, 9));
        data.add(new Entry(5, 2));
        data.add(new Entry(6, 7));
    }

    public MyChartView(Context context) {
        super(context);
    }

    public MyChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private int height;
    private int width;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthsize = MeasureSpec.getSize(widthMeasureSpec);      //取出宽度的确切数值
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);      //取出宽度的测量模式

        int heightsize = MeasureSpec.getSize(heightMeasureSpec);    //取出高度的确切数值
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);    //取出高度的测量模式

        height = heightsize;
        width = widthsize;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);


        canvas.drawLine(30, height - 30, width - 30, height - 30, mPaint);//x
        canvas.drawLine(30, 30, 30, height - 30, mPaint);//Y
        int lineWidth = (width - 30) - 30;
        int duanwidth = lineWidth / data.size();
        int duanheight = lineWidth / 10;
        Float www = 0f;

        Float hhh = Float.valueOf(height-30);
//        List<MyBean> list = new ArrayList<>();
        HashMap<String, Float> hashMapX = new HashMap<>();
        HashMap<String, Float> hashMapY = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            hhh = hhh-duanheight  ;
            canvas.drawText(i + "元", 0, hhh, mPaint);
//            list.add(new MyBean(0, hhh, i));
            hashMapY.put(i + "元",hhh);
        }

        for (Entry datum : data) {
            www = www + duanwidth;
            canvas.drawText(datum.getX() + "号", www, height - 5, mPaint);

            hashMapX.put(datum.getX() +"号", www);

        }
        Path path = new Path();

        for (Entry datum : data) {
            Float aFloat = hashMapX.get(datum.getX() + "号");
            Float aFloat1 = hashMapY.get((int)datum.getY() + "元");
            if (datum.getX()==1){
                path.moveTo(aFloat,aFloat1);
                canvas.drawText(aFloat1+"数据",aFloat,aFloat1-10,mPaintText);
            }else {
                path.lineTo(aFloat,aFloat1);
                canvas.drawText(aFloat1+"数据",aFloat,aFloat1-10,mPaintText);
            }

//            try {
//Unboxing of 'aFloat' may produce 'NullPointerException'
//
//            }catch ( java.lang.NullPointerException e){
//                e.printStackTrace();
//            }
        }
        canvas.drawPath(path,mPaint);

//        canvas.drawText();


//        path.lineTo();

//        path.addRect(0, 0, width, height, Path.Direction.CW);
//        canvas.drawPath(path, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}
